package com.sky.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sky.Dao.DishDao;
import com.sky.Dao.SetmealDao;
import com.sky.Dao.ShoppingCartDao;
import com.sky.Service.ShoppingCartService;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartDao, ShoppingCart> implements ShoppingCartService {

    @Autowired
    private ShoppingCartService shoppingCartService;

    @Autowired
    private ShoppingCartDao shoppingCartDao;

    @Autowired
    private DishDao dishDao;

    @Autowired
    private SetmealDao setmealDao;


    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //先获取当前用户的id
        Long currentUserId = BaseContext.getCurrentUserId();
        //判断当前添加的是菜品还是套餐
        if (shoppingCartDTO.getDishId() != null) {
            //添加过来的是菜品
            //判断当前添加到购物车的菜品在购物车中是否已经存在
            LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
            //菜品的话不仅要判断用户id和菜品id，还要判断菜品的口味，不同的口味也属于不同的菜品
            queryWrapper.eq(ShoppingCart::getUserId, currentUserId)
                    .eq(ShoppingCart::getDishId, shoppingCartDTO.getDishId())
                    .eq(shoppingCartDTO.getDishFlavor() != null, ShoppingCart::getDishFlavor, shoppingCartDTO.getDishFlavor());
            ShoppingCart shoppingCart = shoppingCartDao.selectOne(queryWrapper);
            if (shoppingCart != null) {
                extracted(shoppingCart);
                //更新数据
                shoppingCartDao.updateById(shoppingCart);
            } else {
                //如果不存在，就插入一条新的数据
                ShoppingCart shoppingCart1 = new ShoppingCart();
                Dish dish = dishDao.selectById(shoppingCartDTO.getDishId());
                shoppingCart1.setName(dish.getName());
                shoppingCart1.setUserId(currentUserId);
                shoppingCart1.setDishId(shoppingCartDTO.getDishId());
                shoppingCart1.setDishFlavor(shoppingCartDTO.getDishFlavor());
                shoppingCart1.setNumber(1);
                shoppingCart1.setAmount(dish.getPrice());
                shoppingCart1.setImage(dish.getImage());
                //插入数据
                shoppingCartDao.insert(shoppingCart1);
            }

        } else {
            //添加过来的是套餐
            //判断当前添加到购物车的商品在购物车中是否已经存在
            LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
            //套餐的话只需要判断用户id和套餐id
            queryWrapper.eq(ShoppingCart::getUserId, currentUserId)
                    .eq(ShoppingCart::getSetmealId, shoppingCartDTO.getSetmealId());
            ShoppingCart shoppingCart = shoppingCartDao.selectOne(queryWrapper);

            if (shoppingCart != null) {
                //如果已经存在，只需要将数量加一
                extracted(shoppingCart);
                //更新数据
                shoppingCartDao.updateById(shoppingCart);
            } else {
                //如果不存在，就插入一条新的数据
                ShoppingCart shoppingCart1 = new ShoppingCart();
                Setmeal setmeal = setmealDao.selectById(shoppingCartDTO.getSetmealId());
                shoppingCart1.setName(setmeal.getName());
                shoppingCart1.setUserId(currentUserId);
                shoppingCart1.setSetmealId(shoppingCartDTO.getSetmealId());
                shoppingCart1.setNumber(1);
                shoppingCart1.setAmount(setmeal.getPrice());
                shoppingCart1.setImage(setmeal.getImage());
                //插入数据
                shoppingCartDao.insert(shoppingCart1);
            }
        }
    }

    @Override
    public List<ShoppingCart> getShoppingCartList() {
        //根据当前用户id查询购物车列表
        Long currentUserId = BaseContext.getCurrentUserId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, currentUserId);
        List<ShoppingCart> shoppingCarts = shoppingCartDao.selectList(queryWrapper);
        return shoppingCarts;


    }

    @Override
    public void subShoppingCartItem(ShoppingCartDTO shoppingCartDTO) {
        //先获取当前用户的id
        Long currentUserId = BaseContext.getCurrentUserId();
        //判断当前减少的是菜品还是套餐
        if (shoppingCartDTO.getDishId() != null) {
            //减少的是菜品
            LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
            //菜品的话不仅要判断用户id和菜品id，还要判断菜品的口味，不同的口味也属于不同的菜品
            queryWrapper.eq(ShoppingCart::getUserId, currentUserId)
                    .eq(ShoppingCart::getDishId, shoppingCartDTO.getDishId())
                    .eq(shoppingCartDTO.getDishFlavor() != null, ShoppingCart::getDishFlavor, shoppingCartDTO.getDishFlavor());
            ShoppingCart shoppingCart = shoppingCartDao.selectOne(queryWrapper);
            Integer number = shoppingCart.getNumber();
            if (number == 1) {
                //如果当前商品在购物车中的份数为1，则直接删除该条数据
                shoppingCartDao.deleteById(shoppingCart.getId());
            } else {
                //如果当前商品在购物车中的份数大于1，则减少该条数据中的份数
                shoppingCart.setNumber(shoppingCart.getNumber() - 1);
                shoppingCartDao.updateById(shoppingCart);
            }
        } else {
            //减少的是套餐
            LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
            //套餐的话只需要判断用户id和套餐id
            queryWrapper.eq(ShoppingCart::getUserId, currentUserId)
                    .eq(ShoppingCart::getSetmealId, shoppingCartDTO.getSetmealId());
            ShoppingCart shoppingCart = shoppingCartDao.selectOne(queryWrapper);
            Integer number = shoppingCart.getNumber();
            if (number == 1) {
                //如果当前商品在购物车中的份数为1，则直接删除该条数据
                shoppingCartDao.deleteById(shoppingCart.getId());
            } else {
                //如果当前商品在购物车中的份数大于1，则减少该条数据中的份数
                shoppingCart.setNumber(shoppingCart.getNumber() - 1);
                shoppingCartDao.updateById(shoppingCart);
            }
        }
    }

    @Override
    public void cleanShoppingCart() {
        //先获取当前用户的id
        Long currentUserId = BaseContext.getCurrentUserId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, currentUserId);
        shoppingCartDao.delete(queryWrapper);
    }

    private static void extracted(ShoppingCart shoppingCart) {
        //如果已经存在，只需要将数量加一
        Integer number = shoppingCart.getNumber();
        //更新数量
        shoppingCart.setNumber(number + 1);
    }
}
